package com.adam.management.mapper;

import com.adam.management.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    @Select("select * from user")
    public User getAll();
    @Select("select * from user where username=#{username} and password=#{password}")
    public User findByNameAndPassWord(User user);
    @Select("select * from user where username=#{username}")
    public User findByName(User user);
    @Insert("insert into user(username,password,phone,nickname,createdate) values(#{username},#{password},#{phone},#{nickname},#{createdate});")
    public void createUser(User user);
    @Update("update `user` set `password`=#{password},`nickname` = #{nickname}, `username`=#{username}, `phone`=#{phone} where `id`=#{id}")
    public void updateUser(User user);
    @Select("select * from user")
    public List<User> findAll();
    @Select("<script>"
            +"select count(*) from user"
            +"<where>"
            +"<if test=\"username!=null and username!=''\">"
            +"username like CONCAT('%',#{username},'%')"
            +"</if>"
            +"<if test=\"nickname!=null and nickname!=''\">"
            +"and nickname like CONCAT('%',#{nickname},'%')"
            +"</if>"
            +"</where>"
            +"</script>")
    public int count(User search);
    @Select("<script>"
            +"select * from user"
            +"<where>"
            +"<if test=\"username!=null and username!=''\">"
            +"username like CONCAT('%',#{username},'%')"
            +"</if>"
            +"<if test=\"nickname!=null and nickname!=''\">"
            +"and nickname like CONCAT('%',#{nickname},'%')"
            +"</if>"
            +"</where>"
            +"</script>")
    public List<User> findAllByPage(User search);
    @Delete("delete from user where id=#{id}")
    public void delete(int id);
    @Select("select * from user where id=#{id}")
    public User findById(int id);
}
